/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "Shared/Net/ServerSocket.h"

namespace Shared {
    namespace Net {

        ServerSocket::ServerSocket(struct event_base *evBase, Callback callback, void *userData /* = NULL */)
            : _evBase(evBase)
            , _accepter(NULL)
            , _callback(callback)
            , _userData(userData)
        {

        }

        ServerSocket::~ServerSocket(void)
        {
            uninitialize();
        }

        bool ServerSocket::open(const char *ip, int port, int backlog)
        {
            struct sockaddr_in saddr;
            ::memset(&saddr, 0, sizeof(saddr));
            saddr.sin_family = AF_INET;
#if defined(__WINDOWS__)
            saddr.sin_addr.S_un.S_addr = ::inet_addr(address);
#elif defined(__LINUX__)
            saddr.sin_addr.s_addr = ::inet_addr(address);
#endif
            saddr.sin_port = ::htons(u_short(port));

            _accepter = ::evconnlistener_new_bind(
                _evBase, &ServerSocket::acceptCallback, this,
                LEV_OPT_REUSEABLE | LEV_OPT_CLOSE_ON_FREE, backlog,
                (struct sockaddr*)&saddr,
                sizeof(saddr));
            return _accepter != NULL;
        }

        void ServerSocket::close(void)
        {
			if (_accepter) {
				//disableAccept();
                evconnlistener_free(_accepter);
                _accepter = NULL;
			}
        }

        bool ServerSocket::enableAccept(void)
        {
			SHARED_ASSERT(_accepter);
            return evconnlistener_enable(_accepter) == 0;
        }

        bool ServerSocket::disableAccept(void)
        {
			SHARED_ASSERT(_accepter);
            return evconnlistener_disable(_accepter) == 0;
        }

        void ServerSocket::uninitialize(void)
        {
            if (_accepter)
            {
                evconnlistener_free(_accepter);
                _accepter = NULL;
            }
        }

        void ServerSocket::acceptCallback( struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *sa, int socklen, void *userData )
        {
            ServerSocket *pSS = static_cast<ServerSocket*>(userData);
            SHARED_ASSERT(pSS && pSS->_callback);
            pSS->_callback(fd, sa, socklen, pSS->_userData);
        }
    }
}